/**
 * Created by lynnwang on 5/4/17.
 */


function show_detail(id) {
    window.history.pushState(null, document.title, window.location.href);
    document.location.href = "notificationdetail?notificationId=" + id;

}


//Ajax and load data

$(function () {
    //页数
    var page = 0;
    // 每页展示10个
    var size = 4;
    $('.weui_panel').dropload({
        scrollArea: window,
        autoLoad: true,//自动加载

        domDown: {//上拉
            domClass: 'dropload-down',
            domRefresh: '<div class="dropload-refresh f15 "><i class="icon icon-20"></i>上拉加载更多</div>',
            domLoad: '<div class="dropload-load f15"><span class="weui-loading"></span>正在加载中...</div>',
            domNoData: '<div class="dropload-noData">没有更多数据了</div>'
        },
        domUp: {//下拉
            domClass: 'dropload-up',
            domRefresh: '<div class="dropload-refresh"><i class="icon icon-114"></i>下拉刷新</div>',
            domUpdate: '<div class="dropload-load f15"><i class="icon icon-20"></i>释放更新...</div>',
            domLoad: '<div class="dropload-load f15"><span class="weui-loading"></span>正在加载中...</div>'
        },
        loadUpFn: function (me) {//刷新
            page = 1;
            $.ajax(
                {
                    type: 'POST',
                    url: 'mynotification',
                    data: {
                        wechatid : $("#wechatuser_id").text(),
                        page : page,
                        pagesize : size
                    },
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        var result = '';
                        console.log("success! data.result.length = "+data.result.length);
                        for (var i = 0; i < data.result.length; i++) {
                            console.log("refresh*** data.result[i].title = "+data.result[i].title);
                            result += '<li class="weui-panel weui-panel_access list_item">'
                                + '<div class="weui-panel__bd item_body">'
                                + '<a class="weui-media-box weui-media-box_appmsg item_box" href="javascript:void(0);" onclick="show_detail('+data.result[i].id+')">'
                                + '<div class="color_block weui-updown">' + '</div>'
                                + '<div class="weui-media-box__bd">'
                                + '<h4 class="weui-media-box__title">'+ data.result[i].title + '</h4>'
                                + '<p class="weui-media-box__desc">' + data.result[i].date + '</p>'
                                + '</div>'
                            if (data.result[i].isFinished){
                                console.log("loading*** data.result[i].isFinished = "+data.result[i].isFinished);
                                result += '<div class="notification_picture_d">'
                                    + '<img class="notification_picture" src="./assets/img/finish_stamp2.png"/>'
                                    + '</div>';
                            }
                            result += '</a>'
                                + '</div>';
                            +'</li>';
                        }
                        // 为了测试，延迟1秒加载
                        setTimeout(function () {
                            $('.weui_panel_bd').html(result);
//                                var lazyloadImg = new LazyloadImg({
                            // el: '.weui-updown [data-img]', //匹配元素
                            // top: 50, //元素在顶部伸出长度触发加载机制
                            // right: 50, //元素在右边伸出长度触发加载机制
                            // bottom: 50, //元素在底部伸出长度触发加载机制
                            // left: 50, //元素在左边伸出长度触发加载机制
                            // qriginal: false, // true，自动将图片剪切成默认图片的宽高；false显示图片真实宽高
//                                    load: function (el) {
//                                        el.style.cssText += '-webkit-animation: fadeIn 01s ease 0.2s 1 both;animation: fadeIn 1s ease 0.2s 1 both;';
//                                    },
//                                    error: function (el) {
//
//                                    }
//                                });
                            // 每次数据加载完，必须重置
                            me.resetload();
                            // 重置索引值，重新拼接数据
                            // page = 0;
                            // 解锁
                            me.unlock();
                            me.noData(false);
                        }, 1000);
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!');
                        // 即使加载出错，也得重置
                        me.resetload();
                    }
                });
        },
        loadDownFn: function (me) {//加载更多
            page++;
            window.history.pushState(null, document.title, window.location.href);
            var result = '';
            $.ajax(
                {
                    type: 'POST',
                    url: 'mynotification',
                    data: {
                        wechatid : $("#wechatuser_id").text(),
                        page : page,
                        pagesize : size
                    },
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        var arrLen = data.result.length;
                        console.log("success! data.result.length = "+data.result.length);
                        if (arrLen > 0) {


                            for (var i = 0; i < arrLen; i++) {
                                console.log("loading*** data.result[i].title = "+data.result[i].title);
                                result += '<li class="weui-panel weui-panel_access list_item">'
                                    + '<div class="weui-panel__bd item_body">'
                                    + '<a class="weui-media-box weui-media-box_appmsg item_box" href="javascript:void(0);" onclick="show_detail('+data.result[i].id+')">'
                                    + '<div class="color_block weui-updown">'
                                    + '</div>'
                                    + '<div class="weui-media-box__bd">'
                                    + '<h4 class="weui-media-box__title">' + data.result[i].title + '</h4>'
                                    + '<p class="weui-media-box__desc">' + data.result[i].date + '</p>'
                                    + '</div>'
                                if (data.result[i].isFinished){
                                    console.log("loading*** data.result[i].isFinished = "+data.result[i].isFinished);
                                    result += '<div class="notification_picture_d">'
                                        + '<img class="notification_picture" src="./assets/img/finish_stamp2.png"/>'
                                        + '</div>';
                                }
                                result += '</a>'
                                    + '</div>';
                                +'</li>';
                            }
                            // 如果没有数据
                        } else {
                            console.log("success but no data");
                            // 锁定
                            me.lock();
                            // 无数据
                            me.noData();
                        }

                        // 为了测试，延迟1秒加载
                        setTimeout(function () {
                            $('.weui_panel_bd').append(result);
//                                var lazyloadImg = new LazyloadImg({
                            // el: '.weui-updown [data-img]', //匹配元素
                            // top: 50, //元素在顶部伸出长度触发加载机制
                            // right: 50, //元素在右边伸出长度触发加载机制
                            // bottom: 50, //元素在底部伸出长度触发加载机制
                            // left: 50, //元素在左边伸出长度触发加载机制
                            // qriginal: false, // true，自动将图片剪切成默认图片的宽高；false显示图片真实宽高
//                                    load: function (el) {
//                                        el.style.cssText += '-webkit-animation: fadeIn 01s ease 0.2s 1 both;animation: fadeIn 1s ease 0.2s 1 both;';
//                                    },
//                                    error: function (el) {
//
//                                    }
//                                });
                            //
                            // 每次数据加载完，必须重置
                            me.resetload();
                        }, 1000);
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!');
                        // 即使加载出错，也得重置
                        me.resetload();
                    }
                });
        }
    });


});